local skill = fk.CreateSkill{
  name = "tea__nianzhi"
}
---@type TrigSkelSpec<TrigFunc>
local nianzhi = {
  name = "tea__nianzhi",
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill("tea__nianzhi") or player:isKongcheng() or not player.room.current then return false end
    for _, move in ipairs(data) do
      if move.from == player or (move.toArea == Player.Hand and move.to == player) then
        for _, info in ipairs(move.moveInfo) do
          if (info.fromArea == Player.Hand and (move.from == player))
          or (move.toArea == Player.Hand and move.to == player) then
            local suits_record = {{"log_heart", 0}, {"log_diamond", 0}, {"log_spade", 0}, {"log_club", 0},}
            local sucess = 0
            for _, cid in ipairs(player:getCardIds(Player.Hand)) do
              sucess = 1
              if cid then
                local card = Fk:getCardById(cid)
                local suit = card:getSuitString(true)
                for _, v in ipairs(suits_record) do
                  if v[1] == suit then
                    v[2] = v[2] + 1
                  end
                end
              end 
            end
            local suc = 0
            local suit_num = 0
            local suit_type = {}
            if sucess == 1 then
              for _, n in ipairs(suits_record) do
                if n[2] > 0 then
                  suit_num = suit_num + 1
                  table.insert(suit_type, n[1])
                end
              end
              for _, i in ipairs(suits_record) do
                if i[2] > 0 and suc == 0 then
                  suc = i[2]
                elseif i[2] > 0 and i[2] ~= suc then
                  suc = 0
                  break
                end
              end
            end
            if suc > 0 then event:setCostData(self, {suit_num, suit_type}) end
            return suc > 0
          end
        end
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local current = room.current
    local num = event:getCostData(self)[1]
    local suits = event:getCostData(self)[2]
    player:showCards(player.player_cards[Player.Hand])
    current:drawCards(num)
    if not current.dead then
      local mark = current:getMark("@nianzhi-turn")
      if mark == 0 then
        room:setPlayerMark(current, "@nianzhi-turn", suits)
      else
        for _, suit in ipairs(suits) do
          table.insertIfNeed(mark, suit)
        end
        room:setPlayerMark(current, "@nianzhi-turn", mark)
      end
    end
  end,
}
---@type ProhibitSpec
local nianzhi_prohibit = {
  name = "#tea__nianzhi_prohibit",
  prohibit_use = function(self, player, card)
    if player:getMark("@nianzhi-turn") == 0 or not card then return end
    if table.contains(player:getMark("@nianzhi-turn"), card:getSuitString(true)) then
      local subcards = card:isVirtual() and card.subcards or {card.id}
      return #subcards > 0 and table.every(subcards, function(id)
        return table.contains(player:getCardIds(Player.Hand), id)
      end)
    end
  end,
}
skill:addEffect(fk.AfterCardsMove, nianzhi)
skill:addEffect("prohibit", nianzhi_prohibit)
return skill